<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'login':
      login($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function login($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  $cond_vals = new stdClass();
  $cond_vals->c = 't.email = :v1 AND t.password = :v2';
  $cond_vals->v = array(':v1' => $q->email, ':v2' => md5($q->password));
  $tmp = DbUtils::get($myPdo, 'pz2_profile', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmp) > 0) {
    $_SESSION[ADMIN] = $tmp[0];
    echo json_encode($tmp);
    return;
  }
  echo json_encode(array());
}
?>